const mysql = require('mysql2/promise');
// 导入处理.env文件的模块dotenv
require('dotenv').config();
// 验证环境变量
if (!process.env.DB_HOST || !process.env.DB_USER || !process.env.DB_PASSWORD || !process.env.DB_DATABASE) {
  throw new Error('数据库连接配置缺失，请检查 .env 文件');
}

const pool=mysql.createPool({
  host:process.env.DB_HOST,
  user:process.env.DB_USER,
  password:process.env.DB_PASSWORD,
  database:process.env.DB_DATABASE,
  connectionLimit: 100,
  waitForConnections: true,
  queueLimit: 0
})

// 错误监听
pool.on('error', (err) => {
  console.error('MySQL 连接池错误:', err);
});

//导出
module.exports = pool;